package com.boot.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @author Chunsheng.Zhang
 * @date 2021/1/8 17:22
 * @description : TODO
 */
@Component
public class MailUtil {
    @Autowired
    private JavaMailSenderImpl mailSender;

    /**
     * @description: 发送一个简单的邮件
     * @author: ChunSheng.Zhang
     * @date: 2021/1/11 18:15

     * @return void
     **/
    public void send(){

        //创建一个简单消息
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setTo("18792943389@163.com");//收件人
        mailMessage.setFrom("1529994247@qq.com");//发件人
        mailMessage.setSubject("spring boot学习");//主题
        mailMessage.setText("spring boot整合mail的使用真是简单又好用");//邮件内容
        mailSender.send(mailMessage);
    }


    /**
     * @description: 发送一个复杂的邮件
     * @author: ChunSheng.Zhang
     * @date: 2021/1/11 18:16

     * @return void
     **/
    public void sendComplexMail(){

        MimeMessage mimeMessage = mailSender.createMimeMessage();

        //组装
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);

            //标题和内容
            helper.setSubject("我的标题");
            helper.setText("<p style='color:red'>谢谢你的标题，太美好</p>",true);
            //附件
            helper.addAttachment("desk.jpg",new File("C:\\Users\\Administrator\\Desktop\\imgs\\desk.jpg"));

            //收，发件人
            helper.setTo("18792943389@163.com");
            helper.setFrom("1529994247@qq.com");

        } catch (MessagingException e) {
            e.printStackTrace();
        }

        mailSender.send(mimeMessage);
    }

}
